<?php

namespace App\Http\Requests;

use App\Http\Model\Services;
use Illuminate\Foundation\Http\FormRequest;

class AddUserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'user_name' => 'required|string',
            'service_id' => function ($attribute, $value, $fail) {
                if ($value) {
                    $service_info = Services::find($value);
                    if (!isset($service_info->id)) {
                        return $fail('此服务不存在');
                    }
                }
            }
        ];
    }

    public function messages()
    {
        return [
            'user_name.required' => '用户名称不能为空',
            'user_name.string' => '必须是字符串'
        ];
    }
}
